package com.moon.interview.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moon.interview.common.BaseResponse;
import com.moon.interview.common.ResultUtils;
import com.moon.interview.exception.SysErrorCode;
import com.moon.interview.model.dto.question.QuestionAddRequest;
import com.moon.interview.model.dto.question.QuestionQueryRequest;
import com.moon.interview.model.dto.question.QuestionUpdateRequest;
import com.moon.interview.model.vo.QuestionSimpleVo;
import com.moon.interview.model.vo.QuestionVo;
import com.moon.interview.service.IQuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "题目API")
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    IQuestionService questionService;

    @Operation(summary = "题目vo分页查询")
    @PostMapping("/page/allVo")
    public BaseResponse<Page<QuestionVo>> pageAll(@RequestBody QuestionQueryRequest queryRequest) {
        if (queryRequest == null) {
            return ResultUtils.error(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionService.pageQuestionVo(queryRequest));
    }

    @Operation(summary = "分页查询当前用户题目vo")
    @PostMapping("/page/vo")
    public BaseResponse<Page<QuestionVo>> pageVo(@RequestBody QuestionQueryRequest queryRequest) {
        if (queryRequest == null) {
            return ResultUtils.error(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionService.pageQuestionVoByLoginUser(queryRequest));
    }

    @Operation(summary = "分页查询题目列表")
    @PostMapping("/page/simple")
    public BaseResponse<Page<QuestionSimpleVo>> pageList(@RequestBody QuestionQueryRequest queryRequest) {
        if (queryRequest == null) {
            return ResultUtils.error(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionService.pageQuestionSimpleVo(queryRequest));
    }

    @Operation(summary = "题目详情")
    @GetMapping("/detail")
    public BaseResponse<QuestionVo> detail(@RequestParam Long id) {
        if (id == null) {
            return ResultUtils.error(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionService.getQuestionVo(id));
    }

    @Operation(summary = "更新题目")
    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody QuestionUpdateRequest updateRequest) {
        if (updateRequest == null) {
            return ResultUtils.error(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionService.updateQuestion(updateRequest));
    }

    @Operation(summary = "新增题目")
    @PostMapping("/add")
    public BaseResponse<Boolean> add(@RequestBody QuestionAddRequest addRequest) {
        if (addRequest == null) {
            return ResultUtils.error(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionService.addQuestion(addRequest));
    }

    @Operation(summary = "删除题目")
    @GetMapping("/remove")
    public BaseResponse<Boolean> remove(@RequestParam Long id) {
        if (id == null) {
            return ResultUtils.error(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionService.removeQuestion(id));
    }

    @Operation(summary = "批量删除题目")
    @PostMapping("/remove/batch")
    public BaseResponse<Boolean> removeBatch(List<Long> questionIdList) {
        if (questionIdList == null) {
            return ResultUtils.error(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionService.removeBatchQuestion(questionIdList));
    }

    @Operation(summary = "批量设置题库")
    @PostMapping("/update/batch")
    public BaseResponse<Boolean> updateBatch(List<Long> questionIdList) {
        if (questionIdList == null) {
            return ResultUtils.error(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(questionService.removeBatchQuestion(questionIdList));
    }
}
